<template>
	<view class="page">
		<!-- 地址列表 -->
		<view class="address-list" v-if="pageData.data && pageData.data.length > 0">
			<!-- 地址项 -->
			<view class="address-item" v-for="(item, index) in pageData.data" :key="item.id">
				<view class="address-info" @tap="handleSelectAddress(item)">
					<view class="user-info">
						<text class="name">{{item.name}}</text>
						<text class="phone">{{item.phone}}</text>
						<text v-if="item.isDefault" class="default-tag">默认</text>
					</view>
					<view class="address-detail">
						收货地址：{{item.region}}{{item.address}}
					</view>
				</view>
				<view class="edit-icon" @tap="handleEdit(item, index)">
					<image src="/static/svg/edit-icon.svg" mode="aspectFit"></image>
				</view>
			</view>
		</view>
        
        <!-- 空状态 -->
        <view v-if="!loading && (!pageData.data || pageData.data.length === 0)" class="empty-state">
            <image src="/static/svg/address.svg" mode="aspectFit" class="empty-icon"></image>
            <text class="empty-text">暂无收货地址</text>
        </view>

		<!-- 全局加载状态 -->
		<view v-if="loading && (!pageData.data || pageData.data.length === 0)" class="global-loading">
			<text class="loading-text">加载中...</text>
		</view>

		<!-- 加载状态 -->
		<view class="load-status" v-if="pageData.data && pageData.data.length > 0">
			<view v-if="status === 'loading'" class="loading">
				<text class="loading-text">加载中...</text>
			</view>
			<view v-else-if="status === 'noMore'" class="no-more">
				<text class="no-more-text">没有更多数据了</text>
			</view>
		</view>

		<!-- 底部新建地址按钮 -->
		<view class="btn-container">
			<button class="btn-new-address" @tap="handleAddAddress">新建地址</button>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { usePageData } from "../../../common/usePageData";

// 初始化搜索参数
const searchParams = ref({
	page: 1,
	size: 20
});

// 使用 usePageData 获取分页数据和状态
const { pageData, status, searchList, loading } = usePageData('address_list', searchParams);

// 编辑地址
const handleEdit = (item, index) => {
	uni.navigateTo({
		url: './edit?id=' + item.id
	});
};

// 新建地址
const handleAddAddress = () => {
	uni.navigateTo({
		url: './edit'
	});
};

// 选择地址
const handleSelectAddress = (item) => {
    // 如果是从订单页面跳转来选择地址，则回传地址信息
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2];
    
    if (prevPage && prevPage.route && prevPage.route.includes('order')) {
        uni.setStorageSync('selectedAddress', JSON.stringify(item));
        uni.navigateBack();
    }
};
</script>

<style>
	.page {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;
	}
	
	.address-list {
		padding: 20rpx;
	}
	
	.address-item {
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.address-info {
		flex: 1;
	}
	
	.user-info {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.name {
		font-size: 32rpx;
		font-weight: bold;
		margin-right: 20rpx;
	}
	
	.phone {
		font-size: 28rpx;
		color: #333;
	}
	
	.default-tag {
		font-size: 22rpx;
		color: #fff;
		background-color: #ff6b35;
		padding: 4rpx 10rpx;
		border-radius: 6rpx;
		margin-left: 20rpx;
	}
	
	.address-detail {
		font-size: 28rpx;
		color: #666;
		line-height: 1.4;
	}
	
	.edit-icon {
		width: 40rpx;
		height: 40rpx;
		margin-left: 20rpx;
	}
	
	.edit-icon image {
		width: 100%;
		height: 100%;
	}
	
    .empty-state {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding-top: 200rpx;
    }
    
    .empty-icon {
        width: 200rpx;
        height: 200rpx;
        margin-bottom: 30rpx;
    }
    
    .empty-text {
        font-size: 28rpx;
        color: #999;
    }

	/* 加载状态样式 */
	.load-status {
		padding: 20rpx;
		text-align: center;
	}

	.loading, .no-more {
		padding: 20rpx;
	}

	.loading-text, .no-more-text {
		font-size: 28rpx;
		color: #999;
	}

	/* 全局加载状态 */
	.global-loading {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 400rpx;
	}
    
	.btn-container {
		position: fixed;
		bottom: 30rpx;
		left: 0;
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	
	.btn-new-address {
		background-color: #ff6b35;
		color: #fff;
		border-radius: 50rpx;
		height: 90rpx;
		line-height: 90rpx;
		font-size: 32rpx;
		border: none;
	}
</style>
